package com.un.ebs.customer.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.un.ebs.customer.domain.Customer;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author liubo
 * @since 2019-09-24
 */
@Component
public interface CustomerMapper extends BaseMapper<Customer> {

    /**
     * 不自动按租户过滤
     *
     * @param tenantId
     * @param phone
     * @return
     */
    @SqlParser(filter = true)
    @Select("select * from tb_customer where tenant_id=#{tenantId} and phone=#{phone} or other_phone=#{phone}")
    List<Customer> selectListByPhone(@Param("tenantId") String tenantId, @Param("phone") String phone);

    /**
     * 不自动按租户过滤
     *
     * @param phone
     * @return
     */
    @SqlParser(filter = true)
    @Select("select top 1 * from tb_customer where phone=#{phone} or other_phone=#{phone}")
    Customer selectByPhone(@Param("phone") String phone);

    @SqlParser(filter = true)
    @Select("select top 1 * from tb_customer where tenant_id=#{tenantId} and phone=#{phone} or other_phone=#{phone} and is_delete=0 order by create_time desc")
    Customer selectByPhoneAndTenant(@Param("phone") String phone, @Param("tenantId") String tenantId);

    @SqlParser(filter = true)
    @Select("select top 1 * from tb_customer where tenant_id=#{tenantId} and wechat_id=#{openId} and is_delete=0 order by create_time desc")
    Customer selectByOpenIdAndTenant(@Param("openId") String openId, @Param("tenantId") String tenantId);

    @SqlParser(filter = true)
    @Select("select * from tb_customer where tenant_id=#{tenantId} or user_id=#{userId}")
    List<Customer> selectByUserAndTenant(@Param("userId") String userId, @Param("tenantId") String tenantId);

    @SqlParser(filter = true)
    @Select("select top 1 * from tb_customer where user_id=#{userId} and is_delete=0 order by create_time desc")
    Customer selectByUser(@Param("userId") String userId);

}
